package com.tul.exercise.base.swing.components;

import com.tul.exercise.base.swing.SwingConsole;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.util.Arrays;
import java.util.List;


/**
 * @Description 《java编程思想》第22章 示例17：
 * <p>
 *      tab 页签
 **/
public class TabbedPanel extends JFrame {
    private List<String> flavors = Arrays.asList("Chocolate", "Strawberry",
            "Vanilla", "Mint Chip", "Mocha Almond Fudge", "Rum Raisin", "Mud Pie");

    private JTabbedPane tabs = new JTabbedPane();
    private JTextField text = new JTextField(20);

    public TabbedPanel() {
        int i = 0;
        for (String flavor : flavors) {
            tabs.addTab(flavor, new JButton("Tabbed pane " + i++));
        }
        tabs.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                text.setText("Tab selected: " + tabs.getSelectedIndex());
            }
        });

        add(BorderLayout.SOUTH, text);
        add(tabs);
    }

    public static void main(String[] args) {
        SwingConsole.run(new TabbedPanel(), 400, 250);
    }
}
